<template>
  <gc-dialog
    :title="isEdit ? '编辑工具' : '新增工具'"
    :dialog-visible="true"
    @cancel="handleCancle"
    @ok="handleConfirm"
  >
    <el-form
      ref="formRef"
      label-position="top"
      :model="form"
      :rules="formRules"
    >
      <el-form-item label="工具名称" prop="name">
        <el-input type="text" v-model="form.name"  placeholder="请输入" />
      </el-form-item>
      <el-form-item label="工具分类" prop="classifyId">
        <el-select v-model="form.classifyId" placeholder="请选择">
          <el-option
            v-for="item in classifyOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工具URL" prop="url">
        <el-input type="text" v-model="form.url"  placeholder="请输入" />
      </el-form-item>
      <el-form-item label="工具Logo" prop="logo">
        <GcUploadImage :imageUrl="form.logo" @get-image="(url: string) => form.logo = url" />
      </el-form-item>
      <el-form-item label="工具描述" prop="description">
        <el-input type="textarea" :rows="3" v-model="form.description"  placeholder="请输入" />
      </el-form-item>
    </el-form>
  </gc-dialog>
</template>

<script setup lang="ts" name="OperateToolModal">
import { ref, reactive, computed, watch, type PropType } from 'vue'
import { ruleRequired, ruleMaxLength } from '@/utils/rules'
import type { FormInstance, FormRules } from 'element-plus'
import { apiCreateTool, apiUpdateTool, apiGetToolClassifyList } from '@/http/service'
import { ElMessage } from 'element-plus'
import type { ToolClassify, Tool } from '@/http/type'

const props = defineProps({
  toolInfo: {
    type: Object as PropType<null | Tool>,
    default: () => null
  }
})
const emit = defineEmits(['close-modal'])
const classifyOptions = ref<Array<ToolClassify>>()
const formRef = ref<null | FormInstance>(null)
const form = reactive({
  name: '',
  classifyId: null,
  url: '',
  logo: '',
  description: ''
})
const formRules = reactive<FormRules>({
  name: [ruleRequired('请输入工具名称'), ruleMaxLength(32)],
  classifyId: [ruleRequired('请选择工具分类')],
  url: [ruleRequired('请输入工具URL地址'), ruleMaxLength(255)],
  description: [ruleRequired('请输入工具描述'), ruleMaxLength(255)]
})
const isEdit = computed(() => {
  return Boolean(props.toolInfo)
})

async function getToolClassifyList () {
  const data = await apiGetToolClassifyList<Array<ToolClassify>>()
  classifyOptions.value = data
}
getToolClassifyList()

async function handleConfirm () {
  const valid = await formRef.value?.validate()
  if (valid) {
    const API = isEdit.value ? apiUpdateTool : apiCreateTool
    await API(form)
    ElMessage.success(isEdit.value ? '编辑工具成功' : '新增工具成功')
    emit('close-modal', true)
  }
}

function handleCancle () {
  emit('close-modal')
}

watch(() => props.toolInfo, val => {
  if (val) {
    const { id, name, classifyId, url, description, logo } = val
    Object.assign(form, {
      id, name, classifyId, url, description, logo
    })
  }
}, { immediate: true })
</script>

<style scoped lang="scss"></style>
